package com.ruoyi.ziran.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ziran.domain.ZrPaperItem;
import com.ruoyi.ziran.domain.ZrQuestionBank;
import com.ruoyi.ziran.service.IZrPaperItemService;
import com.ruoyi.ziran.service.IZrQuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ziran.mapper.ZrPaperMapper;
import com.ruoyi.ziran.domain.ZrPaper;
import com.ruoyi.ziran.service.IZrPaperService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 试卷Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class ZrPaperServiceImpl implements IZrPaperService 
{
    @Autowired
    private ZrPaperMapper zrPaperMapper;
    @Autowired
    private IZrPaperItemService iZrPaperItemService;
    @Autowired
    private IZrQuestionBankService iZrQuestionBankService;

    /**
     * 查询试卷
     * 
     * @param id 试卷主键
     * @return 试卷
     */
    @Override
    public ZrPaper selectZrPaperById(Long id)
    {
        ZrPaper zrPaper=zrPaperMapper.selectZrPaperById(id);
        ZrQuestionBank zrQuestionBank=new ZrQuestionBank();
        zrQuestionBank.setPaperId(id);
        List<ZrQuestionBank>  list=iZrQuestionBankService.selectZrQuestionBankList(zrQuestionBank);
        zrPaper.setList(list);
        return zrPaper;
    }

    /**
     * 查询试卷列表
     * 
     * @param zrPaper 试卷
     * @return 试卷
     */
    @Override
    public List<ZrPaper> selectZrPaperList(ZrPaper zrPaper)
    {
        return zrPaperMapper.selectZrPaperList(zrPaper);
    }

    /**
     * 新增试卷
     * 
     * @param zrPaper 试卷
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZrPaper(ZrPaper zrPaper)
    {
        zrPaper.setCreateTime(DateUtils.getNowDate());
        zrPaperMapper.insertZrPaper(zrPaper);
        if (zrPaper.getListId()==null&&zrPaper.getListId().size()==0){
            throw new ServiceException("请选择题");
        }
        //计算分数
        Integer size=zrPaper.getListId().size();
        if (100 % size!=0){
            throw new ServiceException("题的数量只能是被100除尽的数量");
        }
        Integer everyScore=100/size;
        for (Long id:zrPaper.getListId()){
            ZrPaperItem zrPaperItem=new ZrPaperItem();
            zrPaperItem.setPaperId(zrPaper.getId());
            zrPaperItem.setBankId(id);
            zrPaperItem.setEveryScore(new Long(everyScore));
            iZrPaperItemService.insertZrPaperItem(zrPaperItem);
        }
        return 1;
    }

    /**
     * 修改试卷
     * 
     * @param zrPaper 试卷
     * @return 结果
     */
    @Override
    public int updateZrPaper(ZrPaper zrPaper)
    {
        zrPaper.setUpdateTime(DateUtils.getNowDate());
        return zrPaperMapper.updateZrPaper(zrPaper);
    }

    /**
     * 批量删除试卷
     * 
     * @param ids 需要删除的试卷主键
     * @return 结果
     */
    @Override
    public int deleteZrPaperByIds(String ids)
    {
        return zrPaperMapper.deleteZrPaperByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除试卷信息
     * 
     * @param id 试卷主键
     * @return 结果
     */
    @Override
    public int deleteZrPaperById(Long id)
    {
        return zrPaperMapper.deleteZrPaperById(id);
    }
}
